// pages/shouquan/shouquan.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo_score: '',
    isset_Phone: '',
    token: '',
    cache_key: '',
    name:'',
    avatarurl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userInfo"]) {
          that.setData({
            userinfo_score: true
          })
        } else {
          that.setData({
            userinfo_score: false
          })

        }
      }
    })
    wx.getStorage({
      key: 'isset_Phone',
      success:function(res){
        if(res.data == true){
          that.setData({
            isset_Phone:true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindGetUserInfo(e) {
    var that = this
    if (e.detail.errMsg == 'getUserInfo:ok') {
      wx.getUserInfo({
        success:function(res){
          that.data.name = res.userInfo.nickName
          that.data.avatarurl = res.userInfo.avatarUrl
          wx.getStorage({
            key: 'token',
            success: function (res) {
              wx.request({
                url: app.globalData.appurl + 'api/user/changeUserinfo', // 修改用户信息
                data: {
                  name:that.data.name,
                  avatarurl:that.data.avatarurl,
                  token: res.data
                },
                method: "POST",
                // header: {
                //     'content-type': 'application/x-www-form-urlencoded' // 默认值
                // },
                success(res) {
                  if (res.statusCode === 200) {
                    wx.showToast({
                      title: '授权成功', // 标题
                      icon: 'success',  // 图标类型，默认success
                      duration: 1500,  // 提示窗停留时间，默认1500ms
                    })
                    that.onShow()
                  }
                }
              })
            }
          })
        }
      })
      
      
    }
  },
  getPhoneNumber(e) {
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      const that = this
      wx.getStorage({
        key: 'cache_key',
        success: function (res) {
          that.setData({
            cache_key: res.data
          })
        }
      })
      wx.getStorage({
        key: 'token',
        success: function (res) {
          that.setData({
            token: res.data,
          })
          wx.request({
            url: app.globalData.appurl + 'api/user/GetPhoneNumber', // 授权手机
            data: {
              encryptedData: e.detail.encryptedData,
              iv: e.detail.iv,
              cache_key: that.data.cache_key,
              token: that.data.token
            },
            method: "POST",
            // header: {
            //     'content-type': 'application/x-www-form-urlencoded' // 默认值
            // },
            success(res) {
              if (res.statusCode === 200) {
                wx.setStorage({
                  data: true,
                  key: 'isset_Phone',
                  success:function(res)
                  {
                    that.onShow()
                  }
                })
              }
            }
          })
        }
      })
     
    }

  }
})